package test;

import java.util.ArrayList;
import java.util.List;

import base.FractalInst;
import jm.JMC;
import jm.audio.Instrument;
import jm.music.data.Note;
import jm.music.data.Part;
import jm.music.data.Phrase;
import jm.music.data.Score;
import jm.music.tools.Mod;
import jm.util.Play;

// crotchet 四分音符
// Phrase 乐节   phrase.add(note)
// Note 音符   Note n1 = new Note(C4, SN); // C4是音调（音高pitch, int类型），SN是rythmValue（double类型）
//                          八度音分成十二等份，每一份为半音，两个半音相当于全音。 例如：Mod.transpose(phrase, 12); 升八度
//                              
// Score 乐谱，包含多个part
// CPhrase C表示Chord，和弦，CPhrase用于创建和声, 包含一组不同pitch的note，可以和phrrase类似使用（与Phrase类的方法名相同）
// Part 声部  part.add(note),  part.add(phrase) 可以包含phrase或note，往往包含多个phrase, 还包含使用的instrument(乐器）,channel(声道）
// dynamic: 声音强度（音量）int类型, [0,127]之间，最弱-->最响
// tempo:  速度，拍子
// 离线音乐：     note-->phrase-->part-->score
// 在线音乐： note-->RTLine-->RTMixer


// 常用乐器：在ProgramChanges中定义：
// PIANO  --- 钢琴（0）
// EPIANO = 4, ELECTRIC_PIANO = 4, ELPIANO = 4, RHODES = 4, 电钢琴
// NYLON_GUITAR = 24, NGUITAR = 24, GUITAR = 24, ACOUSTIC_GUITAR = 24, AC_GUITAR = 24,  尼龙弦吉他（古典吉他）
//STEEL_GUITAR = 25, SGUITAR = 25, 钢弦吉他
//JAZZ_GUITAR = 26, JGUITAR = 26, CLEAN_GUITAR = 27, 爵士吉他
//CGUITAR = 27, ELECTRIC_GUITAR = 27, EL_GUITAR = 27, 电吉他
// VIOLIN -- 小提琴 40
// VIOLA = 41 中音提琴
// CELLO = 42, VIOLIN_CELLO = 42 大提琴
// SAXOPHONE -- 萨克斯 66
// FLUTE ----横笛 73
//  GUNSHOT--枪响 127

//-------以下为“打击乐器”，需放入channel 10中， 9 = MIDI channel 10
//SLAP_BASS = 36, SBASS = 36, SLAP = 36, SLAP_BASS_1 = 36,   脚大鼓？ 贝司
// octave -- 八度音程 （12pitch)

//http://explodingart.com/jmusic/jmtutorial/t1.html
public class MyTest001 implements JMC {

	public static void main(String[] args) {
		/*
		 * double STACCATO = 0.2;
		 * 
		 * double LEGATO = 0.95;
		 * 
		 * double SOSTENUTO = 1.2;
		 * 
		 * double TENUTO = 1.0;
		 * 
		 * Note n1 = new Note(C4, SN);
		 * 
		 * // Note n2 = new Note(C4, CROTCHET, MF, PAN_CENTRE, CROTCHET * LEGATO); Part
		 * p2 = new Part("Chords", 1, GUITAR);
		 * 
		 * 
		 * // Note n3 = new Note((int) (Math.random() * 126 + 1), SN); Note n3 = new
		 * Note(BS2, SN);
		 * 
		 * Note n = new Note(); n.setPitch(D5);
		 * 
		 * 
		 * int pitchVal = n.getPitch(); pitchVal += 5; n.setPitch(pitchVal);
		 * 
		 * Phrase phrase0 = new Phrase("测试phrase1"); phrase0.add(n1);
		 * 
		 * Phrase phrase2 = new Phrase(); phrase2.add(n3);
		 * 
		 * Mod.quantise(phrase0, DEN);
		 * 
		 * Mod.append(phrase0, phrase2); Mod.repeat(phrase0,12); //Mod.repeat(phrase1);
		 * 
		 * // System.out.println("---------START-----------"); //
		 * System.out.println(phrase0); //
		 * System.out.println("---------END-----------");
		 * 
		 * 
		 * Instrument inst = new FractalInst(44100);
		 */
		
		Score score = new Score("我的谱曲");
		Part part1 = new Part("第一声部",GUITAR,0);//GUITAR,CELLO 第二个参数是“乐器”, int类型，[0,127]
		
		
		
		
		
//		Note note1 = new Note(C4,SN);
//		Note note2 = new Note(CS4, EIGHTH_NOTE);
//		Note note3 = new Note(BS3, DSN);
//		Note note4 = new Note(BS3, TN);
//		Note[] noteArray = new Note[128];
		
//		for (int n=0; n<=127; n++) {
			Phrase phrase1 = new Phrase("乐节1");
			List<Note> noteList = new ArrayList<>();
//			int[] pitchArray=new int[] {C4,D4,E4,F4,G4,A4,B4,C5};
			//音高
			int[] pitches = {A4,G4,E4,D4,C4,  
					         E4,D4,C4,A3,G3,  
					         G3,A3,G3,A3,
					         C4,D4,E4,G4,
					         A4,G4,E4,D4,C4,D4};
			//节奏
			double[] rhythms ={0.75, 0.25, 0.5, 0.5,    2,  
					           0.75, 0.25, 0.5, 0.5,    2, 
					           0.75, 0.25, 0.75, 0.25, 
					           0.75, 0.25, 0.5, 0.5, 
					           0.75, 0.25, 0.25,0.25,0.5,2};
			for(int i=0; i<pitches.length; i++) {
//				noteList.add(new Note(pitchArray[i],0.125));
				noteList.add(new Note(pitches[i], rhythms[i]));
			}
			
			phrase1.addNoteList(noteList.toArray(new Note[0]));
			phrase1.setPan(0.0);
//			phrase1.setInstrument(n); //换乐器
			Mod.repeat(phrase1,2);
			part1.addPhrase(phrase1);
//		}
		
		
		score.add(part1);
		
		Part part2 = new Part("第二声部",FLUTE,1);//GUITAR,CELLO BRIGHTNESS TUBULAR_BELLS第二个参数是“乐器”
		Phrase phrase1_copy= phrase1.copy();
		Mod.transpose(phrase1_copy, 24);
		phrase1_copy.setDynamic(120);
		phrase1_copy.setPan(1.0);
		phrase1_copy.setTempo(FF);
		part2.addPhrase(phrase1_copy);
//		
//		
		score.add(part2);
		
		System.out.println("score="+score);
		Play.midi(score);
		
		
//		Instrument inst = new SawtoothInst(44100);
		
		

	}

}
